home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 3 / Amiga Format CD03 (1996-07-04)(Future Publishing)(GB)(Track 1 of 6)[!][issue 1996-08].iso / pd / utilities / tinymeter / source / tinymeter_main / gaugeclass.c < prev    next >
C/C++ Source or Header  |  1996-05-20  |  27KB  |  1,000 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <dos/dos.h>
  4. #include <proto/SysInfo.h>
  5. #include <libraries/SysInfo.h>
  6. #include <dos/dosextens.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/gadgetclass.h>
  9. #include <intuition/intuitionbase.h>
  10. #include <intuition/classusr.h>
  11. #include <intuition/imageclass.h>
  12. #include <intuition/gadgetclass.h>
  13. #include <intuition/cghooks.h>
  14. #include <intuition/icclass.h>
  15. #include <intuition/classes.h>
  16. #include <intuition/sghooks.h>
  17. #include <intuition/screens.h>
  18. #include <graphics/gfxbase.h>
  19. #include <graphics/text.h>
  20. #include <graphics/gfxmacros.h>
  21. #include <utility/tagitem.h>
  22. #include <utility/hooks.h>
  23. #include <string.h>
  24. #include "gaugeclass.h"
  25. #include "tinymeter.h"
  26.  
  27. struct GaugeData
  28. {
  29.     ULONG   min;
  30.     ULONG   max;
  31.     ULONG   current;
  32.     ULONG   base;
  33.     ULONG   old_cur;
  34.     ULONG   old_bas;
  35.     WORD    labelpos;
  36.     UBYTE   fmtind;
  37.  
  38.     ULONG   type, diswhat;
  39.  
  40.     char    *txtlbl;
  41.     char    *txtfmt;
  42.  
  43.     struct  GAU_Color Colors[GAU_UsedColors];
  44.  
  45.     BOOL    Style3D;
  46.     BOOL    StyleBorder;
  47.     BOOL    StyleBackground;
  48.     BOOL    StyleShadowLabel;
  49.     BOOL    StyleNoGauge;
  50.     BOOL    StyleNoFormat;
  51.     BOOL    StyleNoBase;
  52.  
  53.     struct  TextFont *textFont;
  54.  
  55.     ULONG   Pens[GAU_UsedColors];
  56.     BOOL    PorC[GAU_UsedColors];
  57.  
  58.     BOOL    InitNotDone;
  59.  
  60.     struct  RastPort *nolock_rp;
  61.  
  62.     LONG    histpos,
  63.         oldlgth,
  64.         oldx;
  65.  
  66.     WORD    text_y;
  67.     struct  Locale *locale;
  68.     char    clocktxt[256];
  69.     char    *voltxt;
  70.  
  71.     struct  RastPort   *bg_buffer;
  72.     struct  RastPort   *wk_buffer;
  73.     struct  RastPort   *hs_buffer;
  74.  
  75.     WORD    clockpos;
  76.  
  77.     BOOL    highlight;
  78.     WORD    oldclockx;
  79.     WORD    oldclockw;
  80.  
  81.     ULONG   voltype;
  82.     struct  Screen *scr;
  83. };
  84.  
  85.  
  86. extern struct SysInfo *si;
  87. extern ULONG           executive;
  88. extern struct Library *RetinaBase;
  89. extern ULONG  idle, maximum;
  90. extern ULONG  DoSuperMethodA( struct IClass *cl, Object *obj, Msg message );
  91. extern ULONG  DoMethod( Object *obj, unsigned long MethodID, ... );
  92. extern ULONG  HookEntry();
  93.  
  94. ULONG   dispatchGaugeGadget (Class *cl,Object *o,        Msg msg);
  95.     my_Refresh          (Class *cl,struct Gadget *o, Msg msg);
  96. ULONG   newGauge            (Class *cl,struct Gadget *g,struct gpRender *msg,struct GaugeData *inst);
  97. ULONG   getGauge            (Class *cl,struct Gadget *g,struct gpRender *msg);
  98.     setGauge            (Class *cl,struct Gadget *g,struct gpRender *msg);
  99. ULONG   disposeGauge        (Class *cl,struct Gadget *g,struct gpRender *msg);
  100. ULONG   renderGauge         (Class *cl,struct Gadget *g,struct gpRender *msg);
  101.  
  102. __geta4 ULONG dispatchGaugeGadget(Class *cl,Object *o, Msg msg)
  103. {
  104.     switch( msg->MethodID )
  105.     {
  106.     case    OM_SET:
  107.         setGauge(cl,(struct Gadget *)o,(struct gpRender *)msg);
  108.         return(DoSuperMethodA(cl,o,msg));
  109.     case    GM_RENDER:
  110.         return(renderGauge(cl,(struct Gadget *)o,(struct gpRender *)msg));
  111.     case    OM_NEW:
  112.         if(o = (Object *)DoSuperMethodA(cl, o, msg) )
  113.              return(newGauge(cl,(struct Gadget *)o,(struct gpRender *)msg,INST_DATA(cl, o)));
  114.         else return(0L);
  115.     case    OM_GET:
  116.         return(getGauge(cl,(struct Gadget *)o,(struct gpRender *)msg));
  117.     case    MY_REFRESH:
  118.         my_Refresh(cl,(struct Gadget *)o,msg);
  119.         return(0L);
  120.     case    OM_DISPOSE:
  121.         disposeGauge(cl,(struct Gadget *)o,(struct gpRender *)msg);
  122.     default:
  123.         return(DoSuperMethodA(cl,o,msg));
  124.     }
  125. }
  126.  
  127. Class *initGaugeGadgetClass(void)
  128. {
  129.     struct IClass *cl;
  130.     if( cl = (struct IClass *)MakeClass( NULL, "gadgetclass", NULL, sizeof(struct GaugeData), 0) )
  131.     {  
  132.     cl->cl_Dispatcher.h_Entry    = HookEntry;
  133.     cl->cl_Dispatcher.h_SubEntry = (HOOKFUNC)dispatchGaugeGadget;
  134.     return((Class *)cl);
  135.     }
  136.     return(0L);
  137. }
  138.  
  139. BOOL freeGaugeGadgetClass( Class *cl )
  140. {
  141.     return(FreeClass(cl));
  142. }
  143.  
  144. struct GAU_Color *GetGaugePen(WORD pen)
  145. {
  146.     static struct GAU_Color dummycol={ TRUE, 0, 0, 0 };
  147.     dummycol.red=pen; return(&dummycol);
  148. }
  149.  
  150. getVolsize(struct GaugeData *inst,char *volname)
  151. {
  152.     struct InfoData  inf;
  153.     struct DevProc  *devproc;
  154.     if(devproc=(struct DevProc *)GetDeviceProc(volname,NULL))
  155.     {
  156.     if(DoPkt(devproc->dvp_Port,ACTION_DISK_INFO,MKBADDR(&inf),NULL,NULL,NULL,NULL)==DOSTRUE)
  157.     {
  158.         switch(inf.id_DiskType)
  159.         {
  160.         case    0x42555359:
  161.             inst->max    =0;
  162.             inst->current=GAU_err_busy;
  163.             break;
  164.         case    ID_UNREADABLE_DISK:
  165.             inst->max    =0;
  166.             inst->current=GAU_err_unreadable;
  167.             break;
  168.         case    ID_NOT_REALLY_DOS:
  169.             inst->max    =0;
  170.             inst->current=GAU_err_nodos;
  171.             break;
  172.         case    ID_NO_DISK_PRESENT:
  173.             inst->max    =0;
  174.             inst->current=GAU_err_notpresent;
  175.             break;
  176.         case    ID_KICKSTART_DISK:
  177.             inst->max    =0L;
  178.             inst->current=GAU_err_kickstart;
  179.             break;
  180.         default:
  181.             inst->current=(inf.id_NumBlocks-inf.id_NumBlocksUsed)*inf.id_BytesPerBlock;
  182.             inst->max    =inf.id_NumBlocks*inf.id_BytesPerBlock;
  183.             inst->voltype=inf.id_DiskType;
  184.             break;
  185.         }
  186.     }
  187.     FreeDeviceProc(devproc);
  188.     }
  189.     else
  190.     {
  191.     inst->max=0;
  192.     inst->current=GAU_err_notmount;
  193.     }
  194. }
  195.  
  196. my_Refresh(Class *cl,struct Gadget *g,Msg msg)
  197. {
  198.     struct  GaugeData *inst=INST_DATA(cl,g);
  199.  
  200.     switch (inst->diswhat)
  201.     {
  202.     case    typ_all:
  203.         inst->current=AvailMem(0L);
  204.         break;
  205.     case    typ_chip:
  206.         inst->current=AvailMem(MEMF_CHIP);
  207.         break;
  208.     case    typ_fast:
  209.         inst->current=AvailMem(MEMF_FAST);
  210.         break;
  211.     case    typ_idle:
  212.         switch (executive)
  213.         {
  214.             case    idle_none:
  215.                 inst->current=0L;
  216.                 break;
  217.             case    idle_executive:
  218.                 {
  219.                 struct SI_CpuUsage  cpu;
  220.                 GetCpuUsage(si,&cpu);
  221.                 inst->current=(cpu.used_cputime_lastsec_hz-cpu.used_cputime_lastsec)<<8;
  222.                 }
  223.                 break;
  224.             case    idle_own:
  225.                 inst->current=idle;
  226.                 break;
  227.         }
  228.         if(inst->base<inst->current)inst->base=inst->current;
  229.         break;
  230.     case    typ_largest_total:
  231.         inst->current=AvailMem(MEMF_LARGEST);
  232.         break;
  233.     case    typ_largest_chip:
  234.         inst->current=AvailMem(MEMF_LARGEST|MEMF_CHIP);
  235.         break;
  236.     case    typ_largest_fast:
  237.         inst->current=AvailMem(MEMF_LARGEST|MEMF_FAST);
  238.         break;
  239.     case    typ_largest_retina:
  240.         if(RetinaBase)
  241.             inst->current=Retina_AvailMem(MEMF_LARGEST);
  242.         else
  243.             inst->current=GAU_err_notavail;
  244.         break;
  245.     case    typ_retina:
  246.         if(RetinaBase)
  247.             inst->current=Retina_AvailMem(0L);
  248.         else
  249.             inst->current=GAU_err_notavail;
  250.         break;
  251.     case    typ_volume:
  252.         if(inst->voltxt)getVolsize(inst,inst->voltxt);
  253.         break;
  254.     default:
  255.         break;
  256.     }
  257.     if(msg) renderGauge(cl,g,msg);
  258. }
  259.  
  260. my_Max(Class *cl,struct Gadget *g)
  261. {
  262.     struct  GaugeData *inst=INST_DATA(cl,g);
  263.     switch (inst->diswhat)
  264.     {
  265.     case    typ_largest_total:
  266.     case    typ_all:
  267.         inst->max=AvailMem(MEMF_TOTAL);
  268.         break;
  269.     case    typ_largest_chip:
  270.     case    typ_chip:
  271.         inst->max=AvailMem(MEMF_CHIP|MEMF_TOTAL);
  272.         break;
  273.     case    typ_largest_fast:
  274.     case    typ_fast:
  275.         inst->max=AvailMem(MEMF_FAST|MEMF_TOTAL);
  276.         break;
  277.     case    typ_idle:
  278.         switch (executive)
  279.         {
  280.             case    idle_none:
  281.                 inst->max=0L;
  282.                 break;
  283.             case    idle_executive:
  284.                 {
  285.                 struct SI_CpuUsage  cpu;
  286.                 GetCpuUsage(si,&cpu);
  287.                 inst->max=(cpu.used_cputime_lastsec_hz)<<8;
  288.                 }
  289.                 break;
  290.             case    idle_own:
  291.                 inst->max=maximum;
  292.                 break;
  293.         }
  294.         break;
  295.     case    typ_largest_retina:
  296.     case    typ_retina:
  297.         if(RetinaBase)
  298.             inst->max=Retina_AvailMem(MEMF_TOTAL);
  299.         else
  300.             inst->max=GAU_err_notavail;
  301.         break;
  302.     default:
  303.         break;
  304.     }
  305.     inst->base=inst->current;
  306. }
  307.  
  308.  
  309. ULONG newGauge(Class *cl,struct Gadget *g,struct gpRender *msg,struct GaugeData *inst)
  310. {
  311.     struct   TagItem *ti;
  312.     if(ti = ((struct opSet *)msg)->ops_AttrList)
  313.     {
  314.     if(inst->textFont = (struct TextFont *)GetTagData(GAU_TextFont,OpenTopaz(),ti))
  315.     {
  316.         struct RastPort *dummy;
  317.         if(dummy=(struct RastPort *)pAllocVec(sizeof(struct RastPort)))
  318.         {
  319.         int     i;
  320.         char    *foo;
  321.         UWORD   def_pens[]={ 2, 2, 1, 3, 1, 2, 1, 0 };
  322.         ULONG   gau_tags[]={GAU_ColLabel,GAU_ColFormat,GAU_ColBase,GAU_ColCurrent,GAU_ColNegative,GAU_ColBrightEdg,GAU_ColDarkEdg,GAU_ColBackground};
  323.  
  324.         InitRastPort(dummy);
  325.         dummy->Font=inst->textFont;
  326.  
  327.         inst->txtfmt            = (char *)GetTagData(GAU_TextFormat, "%td", ti);
  328.         inst->txtlbl            = (char *)GetTagData(GAU_Label, "Gauge", ti);
  329.         inst->labelpos          = GetTagData(GAU_LabelPos,TextLength(dummy,inst->txtlbl,my_strlen(inst->txtlbl))+4, ti);
  330.  
  331.         inst->Style3D           = GetTagData(GAU_3D,  TRUE, ti);
  332.         inst->StyleBorder       = GetTagData(GAU_Border, TRUE, ti);
  333.         inst->StyleBackground   = GetTagData(GAU_Background, FALSE, ti);
  334.         inst->StyleShadowLabel  = GetTagData(GAU_ShadowLabel, FALSE, ti);
  335.         inst->StyleNoGauge      = GetTagData(GAU_NoGauge,   FALSE, ti);
  336.         inst->StyleNoFormat     = GetTagData(GAU_NoFormat,  FALSE, ti);
  337.         inst->StyleNoBase       = GetTagData(GAU_NoBase,  FALSE, ti);
  338.         inst->fmtind            = GetTagData(GAU_FmtIndent, FALSE, ti);
  339.         inst->type              = GetTagData(GAU_Type, GAU_Type_gauge, ti);
  340.         inst->diswhat           = GetTagData(GAU_DisWhat, 0,ti);
  341.         inst->voltxt=0L;
  342.         if(foo                  = (char *)GetTagData(GAU_Expansion,NULL,ti))
  343.         {
  344.             if(inst->diswhat==typ_volume)
  345.             {
  346.             inst->voltxt=foo;
  347.             }                    
  348.         }
  349.         for(i=0;i<GAU_UsedColors;i++) CopyMem(GetTagData(gau_tags[i], GetGaugePen(def_pens[i]),ti),&inst->Colors[i],sizeof(struct GAU_Color));
  350.  
  351.         if(inst->diswhat==typ_clock_)
  352.         {
  353.             inst->type      = typ_clock;
  354.             inst->locale    = (struct Locale *)OpenLocale(NULL);
  355.         }
  356.         else inst->locale   = 0L;
  357.  
  358.         inst->InitNotDone=TRUE;
  359.         inst->highlight=TRUE;
  360.         inst->oldclockw=0L;
  361.         inst->oldclockx=0L;
  362.  
  363.         pFreeVec(dummy);
  364.  
  365.         my_Refresh(cl,g,NULL);
  366.         my_Max(cl,g);
  367.  
  368.         return((ULONG)g);
  369.         }
  370.         else return(0L);
  371.     }
  372.     else return(0L);
  373.     }
  374.     else return(0L);
  375. }
  376.  
  377. freeRPorts(struct GaugeData *inst)
  378. {
  379.     if(inst->bg_buffer)
  380.     {
  381.     FreeBitMap(inst->bg_buffer->BitMap);
  382.     pFreeVec(inst->bg_buffer);
  383.     inst->bg_buffer=0L;
  384.     }
  385.  
  386.     if(inst->wk_buffer)
  387.     {
  388.     FreeBitMap(inst->wk_buffer->BitMap);
  389.     pFreeVec(inst->wk_buffer);
  390.     inst->wk_buffer=0L;
  391.     }
  392.  
  393.     if(inst->hs_buffer)
  394.     {
  395.     FreeBitMap(inst->hs_buffer->BitMap);
  396.     pFreeVec(inst->hs_buffer);
  397.     inst->hs_buffer=0L;
  398.     }
  399.  
  400. }
  401.  
  402. ULONG disposeGauge(Class *cl,struct Gadget *g,struct gpRender *msg)
  403. {
  404.     int w;
  405.     struct GaugeData *inst=INST_DATA(cl,g);
  406.     CloseLocale(inst->locale);
  407.     for(w=0;w<GAU_UsedColors;w++)FreePenNew(inst->scr,&inst->Colors[w],w,inst->Pens);
  408.     freeRPorts(inst);
  409. }
  410.  
  411. ULONG getGauge(Class *cl,struct Gadget *g,struct gpRender *msg)
  412. {
  413.     struct GaugeData *inst=INST_DATA(cl,g);
  414.     switch (((struct opGet *)msg)->opg_AttrID)
  415.     {
  416.     case    GAU_RPBackground:
  417.         *(((struct opGet *)msg)->opg_Storage)=(ULONG)inst->bg_buffer;
  418.         break;
  419.     case    GAU_Base:
  420.         *(((struct opGet *)msg)->opg_Storage)=(ULONG)inst->base;
  421.         break;
  422.     case    GAU_Current:
  423.         *(((struct opGet *)msg)->opg_Storage)=(ULONG)inst->current;
  424.         break;
  425.     default:
  426.         *(((struct opGet *)msg)->opg_Storage)=0L;
  427.         break;
  428.     }
  429.     return((ULONG)TRUE);
  430. }
  431.  
  432. setGauge(Class *cl,struct Gadget *g,struct gpRender *msg)
  433. {
  434.     struct TagItem      *ti,*tstate=((struct opSet *)msg)->ops_AttrList;
  435.     struct GaugeData    *inst=INST_DATA(cl,g);
  436.     ULONG  t_data;
  437.  
  438.     while(ti=(struct TagItem *)NextTagItem(&tstate))
  439.     {
  440.     t_data=(ULONG)ti->ti_Data;
  441.     switch(ti->ti_Tag)
  442.     {
  443.         case    GAU_Current:
  444.             inst->current=  (ULONG)t_data;
  445.             break;
  446.         case    GAU_Base:
  447.             inst->base=     (ULONG)t_data;
  448.             break;
  449.         case    GAU_Max:
  450.             inst->max=      (ULONG)t_data;
  451.             break;
  452.         case    GAU_VolType:
  453.             inst->voltype=  (ULONG)t_data;
  454.             break;
  455.     }
  456.     }
  457.     if ( FindTagItem(GA_Width,  ((struct opSet *)msg)->ops_AttrList) ||
  458.      FindTagItem(GA_Height, ((struct opSet *)msg)->ops_AttrList) ||
  459.      FindTagItem(GA_Top,    ((struct opSet *)msg)->ops_AttrList) ||
  460.      FindTagItem(GA_Left,   ((struct opSet *)msg)->ops_AttrList) )
  461.     {
  462.     inst->highlight=TRUE;
  463.     inst->InitNotDone=TRUE;
  464.     freeRPorts(inst);
  465.     }
  466. }
  467.  
  468. char *formattext(ULONG current, ULONG base, ULONG max, char format[], ULONG special, char *temp_str)
  469. {
  470.     int         i=0,
  471.         j=0;
  472.     char        temp_str_1[64];
  473.     BOOL        negative;
  474.  
  475.     while(format[i]!=0)
  476.     {
  477.     if(format[i]=='%')
  478.     {
  479.         ULONG   dummy=0;
  480.         UBYTE   dummy_2=0,
  481.             dummy_1=0,
  482.             dummy_3=0;
  483.         i++;
  484.         if(format[i]=='t'){dummy_2=1;i++;}
  485.         if(format[i]=='T'){dummy_2=4;i++;}
  486.         if(format[i]=='k'){dummy_1=1;i++;}
  487.         if(format[i]=='m'){dummy_1=2;i++;}
  488.         if(format[i]=='o'){dummy_3=1;i++;}
  489.         if(format[i]=='%'){dummy_2=3;i++;}
  490.         switch(format[i])
  491.         {
  492.         case    'd':
  493.             dummy=current;
  494.             negative=FALSE;
  495.             break;
  496.         case    'b':
  497.             dummy=base;
  498.             negative=FALSE;
  499.             break;
  500.         case    'f':
  501.             dummy   = base<current ? current-base   : base-current;
  502.             negative= base<current ? TRUE           : FALSE;
  503.             break;
  504.         case    'a':
  505.             dummy=max;
  506.             negative=FALSE;
  507.             break;
  508.         case    'p':
  509.             dummy_2=2; dummy=current;
  510.             negative=FALSE;
  511.             break;
  512.         case    'v':
  513.             dummy_2=5;
  514.             break;
  515.         default:
  516.             goto noforward;
  517.             break;
  518.         }
  519.         i++;
  520. noforward:
  521.         if(dummy_3==1)dummy=max-dummy;
  522.         if(dummy_1==1)dummy=dummy>>10;
  523.         if(dummy_1==2)dummy=dummy>>20;
  524.         switch(dummy_2)
  525.         {
  526.         case    4:
  527.             dummy_2=long_2_string_with_thousand(dummy,&temp_str_1[0],'.',negative);
  528.             for(dummy_1=0;dummy_1<dummy_2;dummy_1++) temp_str[j++]=temp_str_1[dummy_1];
  529.             break;
  530.         case    1:
  531.             dummy_2=long_2_string_with_thousand(dummy,&temp_str_1[0],',',negative);
  532.             for(dummy_1=0;dummy_1<dummy_2;dummy_1++) temp_str[j++]=temp_str_1[dummy_1];
  533.             break;
  534.         case    2:
  535.             dummy_2=0;
  536.             if(negative) sprintf(&temp_str_1[0],"-%ld",(((dummy>>8)*100)/((max>>8)+1)));
  537.             else         sprintf(&temp_str_1[0],"%ld", (((dummy>>8)*100)/((max>>8)+1)));
  538.             while ((temp_str_1[dummy_2] != 0)&(dummy_2<64)) temp_str[j++]=temp_str_1[dummy_2++];
  539.             break;
  540.         case    0:
  541.             dummy_2=0;
  542.             if(negative) sprintf(&temp_str_1[0],"-%ld",dummy);
  543.             else         sprintf(&temp_str_1[0],"%ld", dummy);
  544.             while ((temp_str_1[dummy_2] != 0)&(dummy_2<64)) temp_str[j++]=temp_str_1[dummy_2++];
  545.             break;
  546.         case    3:
  547.             temp_str[j++]='%';
  548.             break;
  549.         case    5:
  550.             if(special&0xFF000000)temp_str[j++]=(char)((special>>24)&0xFF);
  551.             if(special&0x00FF0000)temp_str[j++]=(char)((special>>16)&0xFF);
  552.             if(special&0x0000FF00)temp_str[j++]=(char)((special>>8) &0xFF);
  553.             temp_str[j++]='\\';
  554.             temp_str[j++]=(char)( special     &0xFF)+0x30;
  555.             break;
  556.         }
  557.     }
  558.     else temp_str[j++]=format[i++];
  559.     }
  560.     temp_str[j++]=0;
  561.     return(temp_str);
  562. }
  563.  
  564. __geta4 putChar_hook(struct Hook *hook,struct Locale *locale,ULONG thechar)
  565. {
  566.     struct GaugeData *data;
  567.     data=(struct GaugeData *)hook->h_Data;
  568.     data->clocktxt[data->clockpos++]=(UBYTE)(thechar);
  569. }
  570.  
  571. struct Hook putCharHook = { { 0,0 },(void *)HookEntry,(void *)putChar_hook, NULL };
  572.  
  573. char *getSTR(struct GaugeData *inst)
  574. {
  575.     static char *errstr[]=
  576.     {
  577.     "Disk is busy",
  578.     "Not mounted",
  579.     "No disk present",
  580.     "Disk unreadable",
  581.     "No DOS format",
  582.     "n.a.",
  583.     "Kickstart disk"
  584.     };
  585.     switch (inst->current-inst->max)
  586.     {
  587.     case    GAU_err_busy:
  588.         return( errstr[0]);
  589.     case    GAU_err_notmount:
  590.         return( errstr[1]);
  591.     case    GAU_err_notpresent:
  592.         return( errstr[2]);
  593.     case    GAU_err_unreadable:
  594.         return( errstr[3]);
  595.     case    GAU_err_nodos:
  596.         return( errstr[4]);
  597.     case    GAU_err_notavail:
  598.         return( errstr[5]);
  599.     case    GAU_err_kickstart:
  600.         return( errstr[6]);
  601.         break;
  602.     default:
  603.         return(formattext(inst->current,inst->base,inst->max,inst->txtfmt,inst->voltype,inst->clocktxt));
  604.     }
  605. }
  606.  
  607. ULONG renderGauge(Class *cl,struct Gadget *g,struct gpRender *msg)
  608. {
  609.     struct  RastPort    *rp;
  610.     struct  BitMap      *work_bm;
  611.     struct  RastPort    *wk_rp,*bg_rp,*hs_rp;
  612.     struct  GaugeData   *inst=INST_DATA(cl,g);
  613.     struct  DateStamp   d_stamp;
  614.     WORD                text_x,w,h;
  615.     WORD                x1,x2,g_x,g_size,g_height,t_length;
  616.     char                *fmt;
  617.     LONG                max;
  618.  
  619.     if((msg->MethodID == MY_REFRESH)&&(((struct my_redraw_msg *)msg)->win))
  620.     {
  621.     rp        = ((struct my_redraw_msg *)msg)->win->RPort;
  622.     inst->scr = ((struct my_redraw_msg *)msg)->win->WScreen;
  623.     }
  624.     else if(msg->gpr_GInfo)
  625.     {
  626.     rp = ( msg->MethodID == GM_RENDER ) ? msg->gpr_RPort : (struct RastPort *) ObtainGIRPort(msg->gpr_GInfo);
  627.     inst->scr=msg->gpr_GInfo->gi_Screen;
  628.     }
  629.     if(rp)
  630.     {
  631.     if(inst->InitNotDone)
  632.     {
  633.         if(work_bm=(struct BitMap *)AllocBitMap(g->Width,g->Height+2,rp->BitMap->Depth,BMF_CLEAR,rp->BitMap))
  634.         {
  635.         if(inst->bg_buffer=(struct RastPort *)pAllocVec(sizeof(struct RastPort)))
  636.         {
  637.             InitRastPort(inst->bg_buffer);
  638.             inst->bg_buffer->BitMap=work_bm;
  639.  
  640.             my_Blit(rp,g->LeftEdge,g->TopEdge,inst->bg_buffer,0,0,g->Width,g->Height);
  641.         }
  642.         else
  643.         {
  644.             FreeBitMap(work_bm);
  645.             return(0L);
  646.         }
  647.         }
  648.         else return(0L);
  649.  
  650.         if(work_bm=(struct BitMap *)AllocBitMap(g->Width,g->Height+2,rp->BitMap->Depth,BMF_CLEAR,inst->bg_buffer->BitMap))
  651.         {
  652.         if(inst->wk_buffer=(struct RastPort *)pAllocVec(sizeof(struct RastPort)))
  653.         {
  654.             InitRastPort(inst->wk_buffer);
  655.             inst->wk_buffer->BitMap=work_bm;
  656.  
  657.             inst->text_y=((g->Height-((inst->textFont->tf_Baseline+inst->textFont->tf_YSize)>>1))>>1);
  658.             if(inst->textFont->tf_Baseline<inst->textFont->tf_YSize)
  659.             {
  660.             inst->text_y+=inst->textFont->tf_Baseline-1;
  661.             if(inst->text_y<inst->textFont->tf_Baseline)inst->text_y=inst->textFont->tf_Baseline;
  662.             }
  663.             else
  664.             {
  665.             inst->text_y+=inst->textFont->tf_YSize-1;
  666.             if(inst->text_y<inst->textFont->tf_YSize)   inst->text_y=inst->textFont->tf_YSize;
  667.             }
  668.  
  669.             SetDrMd(inst->wk_buffer,JAM1);
  670.             SetFont(inst->wk_buffer,inst->textFont);
  671.         }
  672.         else
  673.         {
  674.             FreeBitMap(work_bm);
  675.             return(0L);
  676.         }
  677.         }
  678.         else return(0L);
  679.  
  680.         for(w=0;w<GAU_UsedColors;w++)GetPenNew(inst->scr,&inst->Colors[w],w,inst->PorC,inst->Pens);
  681.  
  682.         if(inst->type==GAU_Type_histmeter)
  683.         {
  684.         if(work_bm=(struct BitMap *)AllocBitMap(g->Width,g->Height+2,rp->BitMap->Depth,BMF_CLEAR,inst->wk_buffer->BitMap))
  685.         {
  686.             if(inst->hs_buffer=(struct RastPort *)pAllocVec(sizeof(struct RastPort)))
  687.             {
  688.             inst->histpos=0;
  689.             InitRastPort(inst->hs_buffer);
  690.             inst->hs_buffer->BitMap=work_bm;
  691.             SetAPen(inst->hs_buffer,inst->Pens[col_bg]);
  692.             if(inst->StyleBackground)
  693.             {
  694.                 my_Blit(inst->bg_buffer,0,0,inst->hs_buffer,0,0,g->Width,g->Height);
  695.             }
  696.             else
  697.             {
  698.                 my_RectFill(inst->hs_buffer,0,0,g->Width,g->Height);
  699.             }
  700.             }
  701.             else
  702.             {
  703.             FreeBitMap(work_bm);
  704.             return(0L);
  705.             }
  706.         }
  707.         else return(0L);
  708.         }
  709.         else inst->hs_buffer=0L;
  710.         inst->old_cur=-1;
  711.         inst->old_bas=-1;
  712.         inst->InitNotDone=FALSE;
  713.     }
  714.  
  715.     wk_rp=inst->wk_buffer;
  716.     bg_rp=inst->bg_buffer;
  717.     switch (inst->type)
  718.     {
  719.         case    GAU_Type_gauge:
  720.             w=g->Width;
  721.             h=g->Height;
  722.             max=inst->max>>8;
  723.             if((inst->current!=inst->old_cur)||(inst->base!=inst->old_bas))
  724.             {
  725.             inst->old_cur=inst->current;
  726.             inst->old_bas=inst->base;
  727.             if(inst->highlight)
  728.             {
  729.                 SetDrMd(rp,JAM1);
  730.                 SetFont(rp,inst->textFont);
  731.                 if(inst->StyleBorder)draw_border_new(rp,inst->labelpos+g->LeftEdge,g->TopEdge,w-inst->labelpos,g->Height,inst->Pens[col_dark],inst->Pens[col_bright]);
  732.                 if(inst->StyleShadowLabel)
  733.                 {
  734.                 SetAPen(rp,inst->Pens[col_dark]);
  735.                 Move(rp,1+g->LeftEdge,inst->text_y+1+g->TopEdge);
  736.                 Text(rp,inst->txtlbl,my_strlen(inst->txtlbl));
  737.                 }
  738.                 SetAPen(rp,inst->Pens[col_label]);
  739.                 Move(rp,g->LeftEdge,inst->text_y+g->TopEdge);
  740.                 Text(rp,inst->txtlbl,my_strlen(inst->txtlbl));
  741.                 inst->highlight=FALSE;
  742.             }
  743.  
  744.             g_x=1+inst->labelpos;
  745.             g_size=w-inst->labelpos-2;
  746.             g_height=h-2;
  747.             my_Blit(bg_rp,g_x,1,wk_rp,g_x,1,g_size,g_height);
  748.  
  749.             if((!inst->StyleNoGauge)&&(inst->current<inst->max))
  750.             {
  751.                 x1=g_size-(WORD)(((inst->base>>8)   *g_size)/((max)==0 ? 1 : (max)));
  752.                 x2=g_size-(WORD)(((inst->current>>8)*g_size)/((max)==0 ? 1 : (max)));
  753.  
  754.                 if(!inst->StyleNoBase)
  755.                 {
  756.                 if(x1<x2)
  757.                 {
  758.                     SetAPen(wk_rp,inst->Pens[col_base]);
  759.                     my_RectFill(wk_rp,g_x,1,x1,g_height);
  760.                     SetAPen(wk_rp,inst->Pens[col_current]);
  761.                     my_RectFill(wk_rp,g_x+x1,1,x2-x1,g_height);
  762.  
  763.                     if(!inst->StyleBackground)
  764.                     {
  765.                     SetAPen(wk_rp,inst->Pens[col_bg]);
  766.                     my_RectFill(wk_rp,g_x+x2,1,g_size-x2,g_height);
  767.                     }
  768.                     if(inst->Style3D)
  769.                     {
  770.                     if(x1>1)   draw_border_new(wk_rp,g_x,1,x1,g_height,inst->Pens[col_bright],inst->Pens[col_dark]);
  771.                     if(x2-x1>1)draw_border_new(wk_rp,g_x+x1,1,x2-x1,g_height,inst->Pens[col_bright],inst->Pens[col_dark]);
  772.                     }
  773.                 }
  774.                 else if(x1>x2)
  775.                 {
  776.                     SetAPen(wk_rp,inst->Pens[col_base]);
  777.                     my_RectFill(wk_rp,g_x,1,x2,g_height);
  778.                     SetAPen(wk_rp,inst->Pens[col_negative]);
  779.                     my_RectFill(wk_rp,g_x+x2,1,x1-x2,g_height);
  780.  
  781.                     if(!inst->StyleBackground)
  782.                     {
  783.                     SetAPen(wk_rp,inst->Pens[col_bg]);
  784.                     my_RectFill(wk_rp,g_x+x1,1,g_size-x1,g_height);
  785.                     }
  786.                     if(inst->Style3D)
  787.                     {
  788.                     if(x2>1)   draw_border_new(wk_rp,g_x,1,x2,g_height,inst->Pens[col_bright],inst->Pens[col_dark]);
  789.                     if(x1-x2>1)draw_border_new(wk_rp,g_x+x2,1,x1-x2,g_height,inst->Pens[col_bright],inst->Pens[col_dark]);
  790.                     }
  791.                 }
  792.                 else
  793.                 {
  794.                     SetAPen(wk_rp,inst->Pens[col_base]);
  795.                     my_RectFill(wk_rp,g_x,1,x1,g_height);
  796.  
  797.                     if(!inst->StyleBackground)
  798.                     {
  799.                     SetAPen(wk_rp,inst->Pens[col_bg]);
  800.                     my_RectFill(wk_rp,g_x+x1,1,g_size-x1,g_height);
  801.                     }
  802.                     if((inst->Style3D)&&(x1>1))draw_border_new(wk_rp,g_x,1,x1,g_height,inst->Pens[col_bright],inst->Pens[col_dark]);
  803.                 }
  804.                 }
  805.                 else
  806.                 {
  807.                 SetAPen(wk_rp,inst->Pens[col_current]);
  808.                 my_RectFill(wk_rp,g_x,1,x2,g_height);
  809.                 if(!inst->StyleBackground)
  810.                 {
  811.                     SetAPen(wk_rp,inst->Pens[col_bg]);
  812.                     my_RectFill(wk_rp,g_x+x2,1,g_size-x2,g_height);
  813.                 }
  814.                 if(inst->Style3D) draw_border_new(wk_rp,g_x,1,x2,g_height,inst->Pens[col_bright],inst->Pens[col_dark]);
  815.                 }
  816.             }
  817.             if(!inst->StyleNoFormat)
  818.             {
  819.                 fmt=getSTR(inst);
  820.                 SetAPen(wk_rp,inst->Pens[col_format]);
  821.                 t_length=TextLength(wk_rp,fmt,my_strlen(fmt));
  822.                 if(t_length<(w-inst->labelpos-1))
  823.                 {
  824.                 switch(inst->fmtind)
  825.                 {
  826.                     case    ind_centered:
  827.                         text_x=g_x+((g_size-t_length)>>1);
  828.                         break;
  829.                     case    ind_left:
  830.                         text_x=g_x+2;
  831.                         break;
  832.                     case    ind_right:
  833.                         text_x=w-t_length-3;
  834.                         break;
  835.                 }
  836.                 Move(wk_rp,text_x,inst->text_y);
  837.                 Text(wk_rp,fmt,my_strlen(fmt));
  838.                 }
  839.             }
  840.             my_Blit(wk_rp,g_x,1,rp,g->LeftEdge+g_x,1+g->TopEdge,g_size,g_height);
  841.             }
  842.             break;
  843.         case    GAU_Type_histmeter:
  844.             max=inst->max>>8;
  845.             w=g->Width;
  846.             h=g->Height;
  847.             g_size=h-1;
  848.             hs_rp=inst->hs_buffer;
  849.             if((!inst->StyleNoGauge)&&(inst->current<inst->max))
  850.             {
  851.             x1=(WORD)(((inst->base   >>8)*(g_size-1))/((max)==0 ? 1 : (max)))+1;
  852.             x2=(WORD)(((inst->current>>8)*(g_size-1))/((max)==0 ? 1 : (max)))+1;
  853.             inst->histpos++;
  854.             if(inst->histpos>w-3)
  855.             {
  856.                 WORD dummy;
  857.                 dummy=w-(w>>1)-2;
  858.                 SetAPen(hs_rp,inst->Pens[col_bg]);
  859.                 if(inst->StyleBackground)
  860.                 {
  861.                 my_Blit(inst->bg_buffer,0,0,inst->hs_buffer,0,0,w,h);
  862.                 my_Blit(inst->bg_buffer,0,0,rp,g->LeftEdge,g->TopEdge,w,h);
  863.                 }
  864.                 else
  865.                 {
  866.                 RectFill(hs_rp,dummy,0,w,h);
  867.                 }
  868.                 inst->histpos=0L;
  869.             }
  870.             if(!inst->StyleNoBase)
  871.             {
  872.                 SetAPen(hs_rp,inst->Pens[col_base]);
  873.                 if(x1>x2)
  874.                 {
  875.                 RectFill(hs_rp,inst->histpos,x1,inst->histpos,g_size);
  876.                 SetAPen(hs_rp,inst->Pens[col_current]);
  877.                 RectFill(hs_rp,inst->histpos,x2,inst->histpos,x1);
  878.                 }
  879.                 else if(x1<x2)
  880.                 {
  881.                 RectFill(hs_rp,inst->histpos,x1,inst->histpos,g_size);
  882.                 SetAPen(hs_rp,inst->Pens[col_negative]);
  883.                 RectFill(hs_rp,inst->histpos,x1,inst->histpos,x2);
  884.                 }
  885.                 else
  886.                 {
  887.                 RectFill(hs_rp,inst->histpos,x1,inst->histpos,g_size);
  888.                 }
  889.             }
  890.             else
  891.             {
  892.                 SetAPen(hs_rp,inst->Pens[col_current]);
  893.                 RectFill(hs_rp,inst->histpos,x2,inst->histpos,g_size);
  894.             }
  895.             my_Blit(hs_rp,inst->histpos,0,rp,inst->histpos+g->LeftEdge,g->TopEdge,1,h-1);
  896.             }
  897.             if(inst->StyleBorder)draw_border_new(rp,g->LeftEdge,g->TopEdge,w,h,inst->Pens[col_dark],inst->Pens[col_bright]);
  898.             if(!inst->StyleNoFormat)
  899.             {
  900.             fmt=getSTR(inst);
  901.             SetAPen(wk_rp,inst->Pens[col_format]);
  902.             t_length=TextLength(wk_rp,fmt,my_strlen(fmt));
  903.             if(t_length<w)
  904.             {
  905.                 UWORD x1,x2,y1,y2;
  906.                 switch(inst->fmtind)
  907.                 {
  908.                 case    ind_centered:
  909.                     text_x=((w-t_length)>>1);
  910.                     break;
  911.                 case    ind_left:
  912.                     text_x=2;
  913.                     break;
  914.                 case    ind_right:
  915.                     text_x=w-t_length-2;
  916.                     break;
  917.                 }
  918.                 if((inst->oldlgth>t_length)||(inst->oldx<text_x))
  919.                 {
  920.                 x1=inst->oldx;
  921.                 x2=inst->oldlgth;
  922.                 }
  923.                 else
  924.                 {
  925.                 x1=text_x;
  926.                 x2=t_length;
  927.                 }
  928.                 y1=h-1-inst->textFont->tf_YSize;
  929.                 y2=inst->textFont->tf_YSize;
  930.                 if((x1>0)&&((y1)>0)&&((x1+x2)<w))
  931.                 {
  932.                 my_Blit(hs_rp,x1,y1,wk_rp,x1,            y1,           x2,y2);
  933.                 Move(wk_rp,text_x,h-3);
  934.                 Text(wk_rp,fmt,my_strlen(fmt));
  935.                 my_Blit(wk_rp,x1,y1,rp,   x1+g->LeftEdge,y1+g->TopEdge,x2,y2);
  936.                 }
  937.                 inst->oldlgth=t_length;
  938.                 inst->oldx=text_x;
  939.             }
  940.             }
  941.             break;
  942.         case    GAU_Type_clock:
  943.             if(inst->clocktxt)
  944.             {
  945.             w=g->Width-1;
  946.             h=g->Height;
  947.             DateStamp((struct DateStamp *)&d_stamp);
  948.             inst->clockpos=0; putCharHook.h_Data=inst; FormatDate(inst->locale,inst->txtfmt,&d_stamp,&putCharHook);
  949.             if((t_length=TextLength(wk_rp,inst->clocktxt,my_strlen(inst->clocktxt)))<w)
  950.             {
  951.                 switch(inst->fmtind)
  952.                 {
  953.                 case    ind_centered:
  954.                     text_x=(w-t_length)>>1;
  955.                     break;
  956.                 case    ind_left:
  957.                     text_x=0;
  958.                     break;
  959.                 case    ind_right:
  960.                     text_x=w-t_length-1;
  961.                     break;
  962.                 }
  963.                 if(inst->oldclockw>t_length)
  964.                 {
  965.                 my_Blit(bg_rp,inst->oldclockx,0,wk_rp,inst->oldclockx,0,inst->oldclockw,h);
  966.                 }
  967.                 else
  968.                 {
  969.                 my_Blit(bg_rp,text_x,0,wk_rp,text_x,0,t_length,h);
  970.                 }
  971.                 if(inst->StyleShadowLabel)
  972.                 {
  973.                 SetAPen(wk_rp,inst->Pens[col_dark]);
  974.                 Move(wk_rp,text_x+1,inst->text_y+1);
  975.                 Text(wk_rp,inst->clocktxt,my_strlen(inst->clocktxt));
  976.                 }
  977.                 SetAPen(wk_rp,inst->Pens[col_format]);
  978.                 Move(wk_rp,text_x,inst->text_y);
  979.                 Text(wk_rp,inst->clocktxt,my_strlen(inst->clocktxt));
  980.                 if(inst->oldclockw>t_length)
  981.                 {
  982.                 my_Blit(wk_rp,inst->oldclockx,0,rp,g->LeftEdge+inst->oldclockx,g->TopEdge,inst->oldclockw,h);
  983.                 }
  984.                 else
  985.                 {
  986.                 my_Blit(wk_rp,text_x,0,rp,g->LeftEdge+text_x,g->TopEdge,t_length,h);
  987.                 }
  988.                 inst->oldclockx=text_x;
  989.                 inst->oldclockw=t_length;
  990.             }
  991.             }
  992.             break;
  993.     }
  994.     if((msg->MethodID !=GM_RENDER)&&(msg->MethodID !=MY_REFRESH))ReleaseGIRPort(rp);
  995.     return(TRUE);
  996.     }
  997.     else return(FALSE);
  998. }
  999.  
  1000.